using System.Collections.Generic;
using Nop.Plugin.Tax.CountryStateZip.Domain;
using Nop.Services.Tax;
using System.Linq;

namespace Nop.Plugin.Tax.CountryStateZip.Services
{
    /// <summary>
    /// Extensions
    /// </summary>
    public static class Extensions
    {
        /// <summary>
        /// Finds tax rate
        /// </summary>
        /// <param name="source">Source</param>
        /// <param name="countryId">Country identifier</param>
        /// <param name="taxCategoryId">Tax category identifier</param>
        /// <returns>Found tax rates</returns>
        public static IList<TaxRate> FindTaxRates(this IList<TaxRate> source,
            int countryId, int taxCategoryId)
        {
            return source
                .Where(x => x.CountryId == countryId && x.TaxCategoryId == taxCategoryId)
                .ToList();
        }

        /// <summary>
        /// Creates TaxSurcharge from TaxRate
        /// </summary>
        /// <param name="source">Source</param>
        /// <returns>Tax surcharges</returns>
        public static IList<TaxSurcharge> ToTaxSurcharges(this IList<TaxRate> source)
        {
            return source
                .Select(x => new TaxSurcharge()
                {
                    CountryId = x.CountryId,
                    IsFixedCharge = x.IsFixedCharge,
                    PercentageOrFee = x.PercentageOrFee,
                    StateProvinceId = x.StateProvinceId,
                    SurchargeTypeId = x.SurchargeTypeId,
                    Zip = x.Zip
                }).ToList();
        }
    }
}
